package com.bank.ui;

import com.bank.dao.CustomerDAO;
import com.bank.ui.CustomerLoginUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RegisterUI extends JFrame {
    private static final Logger LOGGER = Logger.getLogger(RegisterUI.class.getName());
    private final JTextField nameField;
    private final JTextField pidField;
    private final JTextField phoneField;
    private final JTextField addressField;
    private final CustomerLoginUI parent;

    public RegisterUI(CustomerLoginUI parent) {
        this.parent = parent;
        setTitle("客户注册");
        setSize(500, 400); // 增加尺寸以适应更多内容
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10)); // 使用BorderLayout布局

        // 标题
        JLabel titleLabel = new JLabel("客户注册", SwingConstants.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
        add(titleLabel, BorderLayout.NORTH);

        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(6, 2, 10, 10)); // 增加一行用于提示
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加必填项标记
        formPanel.add(new JLabel("姓名*:"));
        nameField = new JTextField();
        formPanel.add(nameField);

        formPanel.add(new JLabel("身份证号*:"));
        pidField = new JTextField();
        formPanel.add(pidField);

        formPanel.add(new JLabel("电话*:"));
        phoneField = new JTextField();
        formPanel.add(phoneField);

        formPanel.add(new JLabel("地址:"));
        addressField = new JTextField();
        formPanel.add(addressField);

        // 提示信息
        formPanel.add(new JLabel(""));
        formPanel.add(new JLabel("* 为必填项"));

        add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(this::register);
        buttonPanel.add(registerButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> {
            if (parent != null) {
                parent.setVisible(true);
            }
            dispose();
        });
        buttonPanel.add(cancelButton);

        // 返回登录按钮
        JButton backToLoginButton = new JButton("返回登录");
        backToLoginButton.addActionListener(e -> {
            if (parent != null) {
                parent.setVisible(true);
            }
            dispose();
        });
        buttonPanel.add(backToLoginButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void register(ActionEvent e) {
        String name = nameField.getText().trim();
        String pid = pidField.getText().trim();
        String phone = phoneField.getText().trim();
        String address = addressField.getText().trim();

        // 验证必填项
        if (name.isEmpty() || pid.isEmpty() || phone.isEmpty()) {
            JOptionPane.showMessageDialog(this, "带*的字段为必填项！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 验证身份证号格式
        if (pid.length() != 18) {
            JOptionPane.showMessageDialog(this, "身份证号必须是18位！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 验证手机号格式
        if (!phone.matches("1[3-9]\\d{9}")) {
            JOptionPane.showMessageDialog(this, "手机号格式不正确！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            // 检查客户是否已存在
            if (CustomerDAO.getCustomerId(name, pid) != -1) {
                JOptionPane.showMessageDialog(this, "该客户已注册！", "注册失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 注册客户插入 userInfo 表(触发器自动触发 → 创建银行卡)


            boolean success = CustomerDAO.registerCustomerBasic(name, pid, phone, address);

            if (success) {
                JOptionPane.showMessageDialog(this, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                if (parent != null) {
                    parent.setVisible(true);
                }
            } else {
                JOptionPane.showMessageDialog(this, "注册失败，身份证号可能已存在！", "注册失败", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            LOGGER.log(Level.SEVERE, "注册失败", ex);
            String errorMessage = "注册失败：" + ex.getMessage();
            if (ex.getMessage().contains("unique constraint") || ex.getMessage().contains("Duplicate entry")) {
                errorMessage += "\n身份证号可能已被使用！";
            }
            JOptionPane.showMessageDialog(this, errorMessage, "数据库错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}